package hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ManageEmployee {
	private static SessionFactory factory;

	public static void main(String[] args) {
		try {
			factory = new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			System.out.println("Failed to create sessionFactory obj. " + ex);
			throw new ExceptionInInitializerError(ex);
		}

		ManageEmployee ME = new ManageEmployee();

		Department dep = ME.addDepartment("Accounting");

		Integer emp1 = ME.addEmployee("Ion", "Ion", 1000, dep);
		Integer emp2 = ME.addEmployee("Gheorghe", "Pop", 2000, dep);
		Integer emp3 = ME.addEmployee("Dan", "Matei", 3000, dep);

		ME.listEmployees();

		ME.updateEmployee(emp1, 3000);
		ME.deleteEmployee(emp2);

		ME.listEmployees();

	}

	public Department addDepartment(String name) {
		Session session = factory.openSession();
		Transaction tx = null;
		Department dep = null;
		Integer depID = null;

		try {
			tx = session.beginTransaction();
			dep = new Department(name);
			depID = (Integer) session.save(dep);
			System.out.println("depID " + depID);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return dep;
	}

	public Integer addEmployee(String fn, String ln, int salary, Department dep) {
		Session session = factory.openSession();
		Transaction tx = null;
		Integer empID = null;

		try {
			tx = session.beginTransaction();
			Employee employee = new Employee(fn, ln, salary, dep);
			empID = (Integer) session.save(employee);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return empID;
	}

	public void updateEmployee(int id, int salary) {
		Session session = factory.openSession();
		Transaction tx = null;

		try {
			tx = session.beginTransaction();

			Employee employee = (Employee) session.get(Employee.class, id);
			employee.setSalary(salary);
			session.update(employee);

			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void deleteEmployee(int id) {
		Session session = factory.openSession();
		Transaction tx = null;

		try {
			tx = session.beginTransaction();

			Employee employee = (Employee) session.get(Employee.class, id);
			session.delete(employee);

			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void listEmployees() {
		Session session = factory.openSession();
		Transaction tx = null;

		try {
			tx = session.beginTransaction();

			List<Employee> employees = session.createQuery("FROM Employee")
					.list();
			for (Employee em : employees) {
				System.out.println("First Name " + em.getfN());
				System.out.println("Last Name " + em.getlN());
				System.out.println("Salary " + em.getSalary());
				System.out.println("Department " + em.getDepartment().getName() + "\n");
			}

			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}
